﻿<%@ WebHandler Language="C#" Class="xinxi_biaoduan" %>

using System;
using System.Web;
using System.Data;
using KeaoBase;
using KeaoDataBase;
using Newtonsoft.Json;
using System.Configuration;
using System.Collections.Generic;

public class xinxi_biaoduan : IHttpHandler {

    public void ProcessRequest (HttpContext context) {
        context.Response.ContentType = "text/plain";
        try
        {
            string uid = context.Request.Form["uid"];
            string kid = context.Request.Form["kid"];
            if (!Auth.UserLogined(uid, kid))
            {
                context.Response.Write("请重试...");
                return;
            }
            string requestType = context.Request.Form["biaoDuanType"];
            string biaoDuanLeiXing = context.Request.Form["biaoDuanLeiXing"];
            if (string.IsNullOrEmpty(requestType) || string.IsNullOrEmpty(biaoDuanLeiXing))
            {
                context.Response.Write("请重试...");
                return;
            }

            BaseDataBase m_DB = System.Reflection.Assembly.Load("KeaoDataBase").CreateInstance(string.Format("KeaoDataBase.{0}", ConfigurationManager.AppSettings["数据库类型"])) as BaseDataBase;
            if (!m_DB.InitDataBase(ConfigurationManager.ConnectionStrings["DataSource"].ConnectionString))
            {
                context.Response.Write("[]");
                return;
            }
            else
            {
                switch (requestType)
                {
                    case "1":
                        string searchCondition = m_DB.CreateAndCondition("biaoduanleixing", DataValueCommonType.字符串, biaoDuanLeiXing, "", SearchConditionType.等于);
                        string riQiKaiShi = context.Request.Form["riQiKaiShi"];
                        string riQiJieShu = context.Request.Form["riQiJieShu"];
                        string zhanDianChaXun = context.Request.Form["zhandian"];
                        if (!string.IsNullOrEmpty(riQiKaiShi) && !string.IsNullOrEmpty(riQiJieShu))
                        {
                            searchCondition += m_DB.CreateAndCondition("riqi", DataValueCommonType.日期时间, riQiKaiShi, riQiJieShu, SearchConditionType.between);
                        }
                        if (!string.IsNullOrEmpty(zhanDianChaXun) && zhanDianChaXun != "—全部—")
                        {
                            searchCondition += m_DB.CreateAndCondition("zhandian", DataValueCommonType.字符串, zhanDianChaXun, "", SearchConditionType.等于);
                        }
                        Dictionary<string, string> orderBys = new Dictionary<string, string>();
                        orderBys.Add("riqi", "asc");
                        DataTable Dt = m_DB.GetDataTableByCondition("tbl_biaoduan", searchCondition,orderBys);
                        Dt.Columns.Add("dian1");
                        Dt.Columns.Add("dian2");
                        Dt.Columns.Add("kaipanriqi");
                        foreach (DataRow row in Dt.Rows)
                        {
                            row["dian1"] = Convert.ToInt32(row["gonglihao1"]) % 1000;
                            row["dian2"] = Convert.ToInt32(row["gonglihao2"]) % 1000;
                            row["gonglihao1"] = Convert.ToInt32(row["gonglihao1"]) / 1000;
                            row["gonglihao2"] = Convert.ToInt32(row["gonglihao2"]) / 1000;
                            row["kaipanriqi"] = Convert.ToDateTime(row["riqi"]).ToString("yyyy-MM-dd");
                        }
                        string jsonStr = JsonConvert.SerializeObject(Dt);
                        context.Response.Write(jsonStr);
                        break;
                    case "2":
                        string gonglihao1 = context.Request.Form["gonglihao1"];
                        string dian1 = context.Request.Form["dian1"];
                        string gonglihao2 = context.Request.Form["gonglihao2"];
                        string dian2 = context.Request.Form["dian2"];
                        string biaoduanmingcheng = string.Format("K{0}{1}-K{2}{3}", gonglihao1, dian1.StartsWith("-") ? dian1 : "+" + dian1, gonglihao2, dian2.StartsWith("-") ? dian2 : "+" + dian2);
                        string biaoduanleixing = biaoDuanLeiXing;
                        string biaoduankuandu = context.Request.Form["kuandu"];
                        string biaoduanhoudu = context.Request.Form["houdu"];
                        string zuoyoufu = context.Request.Form["zuoyoufu"];
                        string biaoDuanID = context.Request.Form["biaoDuanID"];
                        string gongcheng = context.Request.Form["gongcheng"];
                        string riqi = context.Request.Form["riqi"];
                        string ganmidu = context.Request.Form["ganmidu"];
                        string zhandian = context.Request.Form["zhandian"];
                        decimal jihualiang = 0m;
                        decimal fangliang = 0m;
                        string biaoduanchangdu = string.Empty;
                        if (KeaoUtils.isNumber(gonglihao1) && KeaoUtils.isNumber(dian1) && KeaoUtils.isNumber(gonglihao2) && KeaoUtils.isNumber(dian2))
                        {
                            gonglihao1 = (decimal.Parse(gonglihao1) * 1000m + decimal.Parse(dian1)).ToString("0.00");
                            gonglihao2 = (decimal.Parse(gonglihao2) * 1000m + decimal.Parse(dian2)).ToString("0.00");
                            biaoduanchangdu = Math.Abs(decimal.Parse(gonglihao2) - decimal.Parse(gonglihao1)).ToString();
                        }
                        else
                        {
                            context.Response.Write("请重试...");
                            return;
                        }
                        if (KeaoUtils.isNumber(biaoduanchangdu) && KeaoUtils.isNumber(biaoduankuandu) && KeaoUtils.isNumber(biaoduanhoudu) && KeaoUtils.isNumber(ganmidu))
                        {
                            fangliang = decimal.Parse(biaoduanchangdu) * decimal.Parse(biaoduankuandu) * decimal.Parse(biaoduanhoudu);
                            jihualiang = fangliang * decimal.Parse(ganmidu);
                        }
                        else
                        {
                            context.Response.Write("请重试...");
                            return;
                        }


                        if (string.IsNullOrEmpty(biaoDuanID))
                        {
                            //新增
                            TableRow newRow = new TableRow("tbl_biaoduan", Guid.NewGuid().ToString(), DBCtl.插入);
                            TableCol col1 = new TableCol(); col1.ColName = "biaoduanmingcheng"; col1.ValueMember = biaoduanmingcheng; col1.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col1);
                            TableCol col2 = new TableCol(); col2.ColName = "biaoduanleixing"; col2.ValueMember = biaoduanleixing; col2.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col2);
                            TableCol col3 = new TableCol(); col3.ColName = "biaoduanchangdu"; col3.ValueMember = biaoduanchangdu; col3.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col3);
                            TableCol col4 = new TableCol(); col4.ColName = "biaoduankuandu"; col4.ValueMember = biaoduankuandu; col4.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol(); col5.ColName = "shangchuanzhuangtai"; col5.ValueMember = "1"; col5.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol(); col6.ColName = "zhandian"; col6.ValueMember = zhandian; col6.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col6);
                            TableCol col7 = new TableCol(); col7.ColName = "kehubianhao"; col7.ValueMember = ConfigurationManager.AppSettings["客户编号"]; col7.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col7);
                            TableCol col8 = new TableCol(); col8.ColName = "biaoduanhoudu"; col8.ValueType = DataValueCommonType.数字; col8.ValueMember = biaoduanhoudu;
                            newRow.DataCols.Add(col8);
                            TableCol col9 = new TableCol(); col9.ColName = "zuoyoufu"; col9.ValueType = DataValueCommonType.字符串; col9.ValueMember = zuoyoufu;
                            newRow.DataCols.Add(col9);
                            TableCol col10 = new TableCol(); col10.ColName = "gonglihao1"; col10.ValueType = DataValueCommonType.数字; col10.ValueMember = gonglihao1;
                            newRow.DataCols.Add(col10);
                            TableCol col11 = new TableCol(); col11.ColName = "gonglihao2"; col11.ValueType = DataValueCommonType.数字; col11.ValueMember = gonglihao2;
                            newRow.DataCols.Add(col11);
                            TableCol col12 = new TableCol(); col12.ColName = "riqi"; col12.ValueMember = riqi; col12.ValueType = DataValueCommonType.日期时间;
                            newRow.DataCols.Add(col12);
                            TableCol col13 = new TableCol(); col13.ColName = "fangliang"; col13.ValueMember = fangliang; col13.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col13);
                            TableCol col14 = new TableCol(); col14.ColName = "ganmidu"; col14.ValueMember = ganmidu; col14.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col14);
                            TableCol col15 = new TableCol(); col15.ColName = "jihualiang"; col15.ValueMember = jihualiang; col15.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col15);
                            TableCol col16 = new TableCol(); col16.ColName = "gongcheng"; col16.ValueMember = gongcheng; col16.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col16);
                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        else
                        {
                            //修改 
                            TableRow newRow = new TableRow("tbl_biaoduan", biaoDuanID, DBCtl.修改);
                            TableCol col1 = new TableCol(); col1.ColName = "biaoduanmingcheng"; col1.ValueMember = biaoduanmingcheng; col1.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col1);
                            TableCol col2 = new TableCol(); col2.ColName = "biaoduanleixing"; col2.ValueMember = biaoduanleixing; col2.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col2);
                            TableCol col3 = new TableCol(); col3.ColName = "biaoduanchangdu"; col3.ValueMember = biaoduanchangdu; col3.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col3);
                            TableCol col4 = new TableCol(); col4.ColName = "biaoduankuandu"; col4.ValueMember = biaoduankuandu; col4.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col4);
                            TableCol col5 = new TableCol(); col5.ColName = "shangchuanzhuangtai"; col5.ValueMember = "1"; col5.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col5);
                            TableCol col6 = new TableCol(); col6.ColName = "zhandian"; col6.ValueMember = zhandian; col6.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col6);
                            TableCol col7 = new TableCol(); col7.ColName = "kehubianhao"; col7.ValueMember = ConfigurationManager.AppSettings["客户编号"]; col7.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col7);
                            TableCol col8 = new TableCol(); col8.ColName = "biaoduanhoudu"; col8.ValueType = DataValueCommonType.数字; col8.ValueMember = biaoduanhoudu;
                            newRow.DataCols.Add(col8);
                            TableCol col9 = new TableCol(); col9.ColName = "zuoyoufu"; col9.ValueType = DataValueCommonType.字符串; col9.ValueMember = zuoyoufu;
                            newRow.DataCols.Add(col9);
                            TableCol col10 = new TableCol(); col10.ColName = "gonglihao1"; col10.ValueType = DataValueCommonType.数字; col10.ValueMember = gonglihao1;
                            newRow.DataCols.Add(col10);
                            TableCol col11 = new TableCol(); col11.ColName = "gonglihao2"; col11.ValueType = DataValueCommonType.数字; col11.ValueMember = gonglihao2;
                            newRow.DataCols.Add(col11);
                            TableCol col12 = new TableCol(); col12.ColName = "riqi"; col12.ValueMember = riqi; col12.ValueType = DataValueCommonType.日期时间;
                            newRow.DataCols.Add(col12);
                            TableCol col13 = new TableCol(); col13.ColName = "fangliang"; col13.ValueMember = fangliang; col13.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col13);
                            TableCol col14 = new TableCol(); col14.ColName = "ganmidu"; col14.ValueMember = ganmidu; col14.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col14);
                            TableCol col15 = new TableCol(); col15.ColName = "jihualiang"; col15.ValueMember = jihualiang; col15.ValueType = DataValueCommonType.数字;
                            newRow.DataCols.Add(col15);
                            TableCol col16 = new TableCol(); col16.ColName = "gongcheng"; col16.ValueMember = gongcheng; col16.ValueType = DataValueCommonType.字符串;
                            newRow.DataCols.Add(col16);
                            if (m_DB.SaveTableRows(newRow) > 0)
                            {
                                context.Response.Write("OK");
                            }
                            else
                            {
                                context.Response.Write("Failed");
                            }
                        }
                        break;
                    case "3":
                        string delBiaoDuanID = context.Request.Form["biaoDuanID"];
                        if (string.IsNullOrEmpty(delBiaoDuanID))
                        {
                            context.Response.Write("没有找到桩号信息，请重试...");
                            return;
                        }
                        TableRow delRow = new TableRow();
                        delRow.TableName = "tbl_biaoduan";
                        delRow.ID = delBiaoDuanID;
                        if (m_DB.DeleteRowByID(delRow) > 0)
                        {
                            context.Response.Write("OK");
                        }
                        else
                        {
                            context.Response.Write("Failed");
                        }
                        break;
                }
            }
        }
        catch (Exception err)
        {
            KeaoUtils.LogTxt(string.Format("标段路基信息异常。{0}", err.Message));
            context.Response.Write("请重试...");
        }
    }

    public bool IsReusable {
        get {
            return false;
        }
    }

}